from django import forms
from .models import User,Book

class AddBookForm(forms.ModelForm):
    def clean_page(self):
        page = self.cleaned_data.get('page')
        if page >100:
            raise forms.ValidationError("页数不能大于100")
        return page
    class Meta:
        model = Book
        # fields = "__all__"
        # fields = ['title','page']
        #         # exclude = ['price']
        error_messages = {
            'page':{
                'required':'page字段必须填写',
                'invalid':'请传入可用的page参数',
            },
            'title': {
                'max_length': '长度不能超过100',
            },
            'price': {
                'max_value': '价格不能超过1000元',
            }
        }

class RegisterForm(forms.ModelForm):
    pwd1 = forms.CharField(max_length=30)
    pwd2 = forms.CharField(max_length=30)

    def clean(self):
        cleaned_data = super().clean()
        pwd1 = self.cleaned_data.get('pwd1')
        pwd2 = self.cleaned_data.get('pwd2')
        if pwd1 != pwd2:
            raise forms.ValidationError("两次密码输入不一致")

    class Meta:
        model = User
        exclude = ['password']